using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace TelnetExpect
{
    /// <summary>
    /// Provides Telnet communications and in negotiation agrees a fixed set of options.
    /// </summary>
    [Obsolete("Use TelnetStream.  There's no reason to use this class, it was just an interim measure before we implemented TelnetStream itself.", true)]
    public class TelnetAgreeToSomeStream : TelnetBaseStream
    {
        public TelnetAgreeToSomeStream(Stream peer)
            : base(peer)
        {
        }

        readonly TelnetOption[] WillCommandsToAgree = {
            TelnetOption.SuppressGoAhead,
            TelnetOption.Echo,
        };

        List<TelnetOption> DoCommandsToAgree = new List<TelnetOption>();

        protected override void HandleNegotiation(Command command, TelnetOption option)
        {
            switch (command) {
                case Command.Do:
                    if (DoCommandsToAgree.Contains(option)) {
                        AddResponseCommand(Command.Will, option);
                    } else {
                        AddResponseCommand(Command.Wont, option);
                    }
                    break;
                case Command.Dont: // Always agree, but need to answer in most cases.
                    if (DoCommandsToAgree.Contains(option)) {
                        // Presuming we have previously Will'd to that option......
                        // (BTW when we're in state OFF and we see DONT we should sending NOTHING).
                        AddResponseCommand(Command.Wont, option);
                    }
                    break;
                case Command.Will:
                    if (Array.IndexOf(WillCommandsToAgree, option) != -1) {
                        AddResponseCommand(Command.Do, option);
                    } else {
                        AddResponseCommand(Command.Dont, option);
                    }
                    break;
                default: // WONT
                    Debug.Assert(Command.Wont == command, "More than four command types?!?");
                    break;
            }
        }
    }

}
